package com.aifund.admin.interceptor;

import com.aifund.common.entity.TokenEntity;
import com.aifund.common.enums.CommonEnum;
import com.aifund.common.enums.redis.RedisSetKey;
import com.aifund.common.result.ResultBody;
import com.aifund.common.service.SystemConfigService;
import com.aifund.common.util.RedisUtil;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:拦截器
 * @author: Yangxf
 * @date: 2019/4/14 12:58
 */
public class LoginInterceptor implements HandlerInterceptor {

    //提供查询
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {}
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {}
    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        //此处为不需要登录的接口放行
        if (arg0.getRequestURI().contains("/api/login/**") || arg0.getRequestURI().contains("/error") || arg0.getRequestURI().contains("/static")) {
            return true;
        }
        arg1.setCharacterEncoding("UTF-8");
        arg1.setContentType("application/json;;charset=UTF-8");
        ServletOutputStream resultWriter = arg1.getOutputStream();
        if(arg0.getSession().getAttribute("admin")==null){
            ResultBody resultBody = ResultBody.error(CommonEnum.SIGNATURE_NOT_MATCH.getResultCode(),"未登录");
            resultWriter.write(JSON.toJSONString(resultBody).getBytes("UTF-8"));
            resultWriter.flush();
            resultWriter.close();
            return false;
        }
        return true;
    }

}
